﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 软件的邮箱类，用于发送邮箱数据
	/// </summary>
	/// <remarks>
	/// 如果您想实现自己的邮件发送中心，就可以去对应的邮件服务器注册，如果是想快速实现邮件的发送，本系统提供了2个静态的已经注册好了的邮箱发送器。
	/// </remarks>
	/// <example>
	/// 以下的代码演示了通过静态的发送对象来发送邮件，包含了发送普通的邮件，和发送html邮件。
	/// <code lang="cs" source="TestProject\HslCommunicationDemo\FormMail.cs" region="SoftMail" title="SoftMail示例" />
	/// </example>
	// Token: 0x02000194 RID: 404
	public class SoftMail
	{
		/// <summary>
		/// 系统连续发送失败的次数，为了不影响系统，连续三次失败就禁止发送
		/// </summary>
		// Token: 0x170006AE RID: 1710
		// (get) Token: 0x060020C2 RID: 8386 RVA: 0x0009F780 File Offset: 0x0009D980
		// (set) Token: 0x060020C3 RID: 8387 RVA: 0x0009F787 File Offset: 0x0009D987
		private static long SoftMailSendFailedCount { get; set; } = 0L;

		/// <summary>
		/// 实例化一个邮箱发送类，需要指定初始化信息
		/// </summary>
		/// <param name="mailIni">初始化的方法</param>
		/// <param name="addr_From">发送地址，应该和账户匹配</param>
		/// <param name="addr_to">邮件接收地址</param>
		/// <remarks>
		/// 初始化的方法比较复杂，需要参照示例代码。
		/// </remarks>
		/// <example>
		/// <code lang="cs" source="HslCommunication_Net45\BasicFramework\SoftMail.cs" region="Static Mail" title="SoftMail示例" />
		/// </example>
		// Token: 0x060020C4 RID: 8388 RVA: 0x0009F790 File Offset: 0x0009D990
		public SoftMail(Action<SmtpClient> mailIni, string addr_From = "", string addr_to = "")
		{
			this.smtpClient = new SmtpClient();
			mailIni(this.smtpClient);
			this.MailFromAddress = addr_From;
			this.MailSendAddress = addr_to;
		}

		/// <summary>
		/// 系统的邮件发送客户端
		/// </summary>
		// Token: 0x170006AF RID: 1711
		// (get) Token: 0x060020C5 RID: 8389 RVA: 0x0009F7E4 File Offset: 0x0009D9E4
		// (set) Token: 0x060020C6 RID: 8390 RVA: 0x0009F7EC File Offset: 0x0009D9EC
		private SmtpClient smtpClient { get; set; }

		/// <summary>
		/// 发送邮件的地址
		/// </summary>
		// Token: 0x170006B0 RID: 1712
		// (get) Token: 0x060020C7 RID: 8391 RVA: 0x0009F7F5 File Offset: 0x0009D9F5
		// (set) Token: 0x060020C8 RID: 8392 RVA: 0x0009F7FD File Offset: 0x0009D9FD
		private string MailFromAddress { get; set; } = "";

		/// <summary>
		/// 邮件发送的地址
		/// </summary>
		// Token: 0x170006B1 RID: 1713
		// (get) Token: 0x060020C9 RID: 8393 RVA: 0x0009F806 File Offset: 0x0009DA06
		// (set) Token: 0x060020CA RID: 8394 RVA: 0x0009F80E File Offset: 0x0009DA0E
		public string MailSendAddress { get; set; } = "";

		// Token: 0x060020CB RID: 8395 RVA: 0x0009F818 File Offset: 0x0009DA18
		private string GetExceptionMail(Exception ex)
		{
			return string.Concat(new string[]
			{
				StringResources.Language.Time,
				DateTime.Now.ToString(),
				Environment.NewLine,
				StringResources.Language.SoftWare,
				ex.Source,
				Environment.NewLine,
				StringResources.Language.ExceptionMessage,
				ex.Message,
				Environment.NewLine,
				StringResources.Language.ExceptionType,
				ex.GetType().ToString(),
				Environment.NewLine,
				StringResources.Language.ExceptionStackTrace,
				ex.StackTrace,
				Environment.NewLine,
				StringResources.Language.ExceptionTargetSite,
				ex.TargetSite.Name
			});
		}

		/// <summary>
		/// 发生BUG至邮件地址，需要提前指定发送地址，否则失败
		/// </summary>
		/// <param name="ex">异常的BUG，同样试用兼容类型</param>
		/// <returns>是否发送成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020CC RID: 8396 RVA: 0x0009F8FC File Offset: 0x0009DAFC
		public bool SendMail(Exception ex)
		{
			return this.SendMail(ex, "");
		}

		/// <summary>
		/// 发送邮件至地址，需要提前指定发送地址，否则失败
		/// </summary>
		/// <param name="subject">邮件主题</param>
		/// <param name="body">邮件内容</param>
		/// <returns>是否发送成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020CD RID: 8397 RVA: 0x0009F91C File Offset: 0x0009DB1C
		public bool SendMail(string subject, string body)
		{
			return this.SendMail(this.MailSendAddress, subject, body);
		}

		/// <summary>
		/// 发送邮件至地址，需要提前指定发送地址，否则失败
		/// </summary>
		/// <param name="subject">邮件主题</param>
		/// <param name="body">邮件内容</param>
		/// <param name="isHtml">是否是html格式化文本</param>
		/// <returns>是否发送成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020CE RID: 8398 RVA: 0x0009F93C File Offset: 0x0009DB3C
		public bool SendMail(string subject, string body, bool isHtml)
		{
			return this.SendMail(this.MailSendAddress, subject, body, isHtml);
		}

		/// <summary>
		/// 发生BUG至邮件地址，需要提前指定发送地址，否则失败
		/// </summary>
		/// <param name="ex">异常的BUG，同样试用兼容类型</param>
		/// <param name="addtion">额外信息</param>
		/// <returns>是否发送成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020CF RID: 8399 RVA: 0x0009F960 File Offset: 0x0009DB60
		public bool SendMail(Exception ex, string addtion)
		{
			bool flag = string.IsNullOrEmpty(this.MailSendAddress);
			return !flag && this.SendMail(this.MailSendAddress, StringResources.Language.BugSubmit, string.IsNullOrEmpty(addtion) ? this.GetExceptionMail(ex) : ("User：" + addtion + Environment.NewLine + this.GetExceptionMail(ex)));
		}

		/// <summary>
		/// 发送邮件的方法，需要指定接收地址，主题及内容
		/// </summary>
		/// <param name="addr_to">接收地址</param>
		/// <param name="subject">邮件主题</param>
		/// <param name="body">邮件内容</param>
		/// <returns>是否发送成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020D0 RID: 8400 RVA: 0x0009F9C4 File Offset: 0x0009DBC4
		public bool SendMail(string addr_to, string subject, string body)
		{
			return this.SendMail(addr_to, subject, body, false);
		}

		/// <summary>
		/// 发送邮件的方法，默认发送别名，优先级，是否HTML
		/// </summary>
		/// <param name="addr_to">接收地址</param>
		/// <param name="subject">邮件主题</param>
		/// <param name="body">邮件内容</param>
		/// <param name="isHtml">是否是html格式的内容</param>
		/// <returns>是否发送成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020D1 RID: 8401 RVA: 0x0009F9E0 File Offset: 0x0009DBE0
		public bool SendMail(string addr_to, string subject, string body, bool isHtml)
		{
			return this.SendMail(this.MailFromAddress, StringResources.Language.MailServerCenter, new string[]
			{
				addr_to
			}, subject, body, MailPriority.Normal, isHtml);
		}

		/// <summary>
		/// 发送邮件的方法，需要提供完整的参数信息
		/// </summary>
		/// <param name="addr_from">发送地址</param>
		/// <param name="name">发送别名</param>
		/// <param name="addr_to">接收地址</param>
		/// <param name="subject">邮件主题</param>
		/// <param name="body">邮件内容</param>
		/// <param name="priority">优先级</param>
		/// <param name="isHtml">邮件内容是否是HTML语言</param>
		/// <returns>发生是否成功，内容不正确会被视为垃圾邮件</returns>
		// Token: 0x060020D2 RID: 8402 RVA: 0x0009FA18 File Offset: 0x0009DC18
		public bool SendMail(string addr_from, string name, string[] addr_to, string subject, string body, MailPriority priority, bool isHtml)
		{
			bool flag = SoftMail.SoftMailSendFailedCount > 10L;
			bool result;
			if (flag)
			{
				SoftMail.SoftMailSendFailedCount += 1L;
				result = true;
			}
			else
			{
				using (MailMessage mailMessage = new MailMessage())
				{
					try
					{
						mailMessage.From = new MailAddress(addr_from, name, Encoding.UTF8);
						foreach (string addresses in addr_to)
						{
							mailMessage.To.Add(addresses);
						}
						mailMessage.Subject = subject;
						mailMessage.Body = body;
						MailMessage mailMessage2 = mailMessage;
						mailMessage2.Body = string.Concat(new string[]
						{
							mailMessage2.Body,
							Environment.NewLine,
							Environment.NewLine,
							Environment.NewLine,
							StringResources.Language.MailSendTail
						});
						mailMessage.SubjectEncoding = Encoding.UTF8;
						mailMessage.BodyEncoding = Encoding.UTF8;
						mailMessage.Priority = priority;
						mailMessage.IsBodyHtml = isHtml;
						this.smtpClient.Send(mailMessage);
						SoftMail.SoftMailSendFailedCount = 0L;
						result = true;
					}
					catch (Exception ex)
					{
						Console.WriteLine(SoftBasic.GetExceptionMessage(ex));
						SoftMail.SoftMailSendFailedCount += 1L;
						result = false;
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 系统提供一个默认的163邮箱发送账号，只要更改接收地址即可发送服务，可能会被拦截
		/// </summary>
		// Token: 0x040007C0 RID: 1984
		public static SoftMail MailSystem163 = new SoftMail(delegate(SmtpClient mail)
		{
			mail.Host = "smtp.163.com";
			mail.UseDefaultCredentials = true;
			mail.EnableSsl = true;
			mail.Port = 25;
			mail.DeliveryMethod = SmtpDeliveryMethod.Network;
			mail.Credentials = new NetworkCredential("softmailsendcenter", "zxcvbnm6789");
		}, "softmailsendcenter@163.com", "hsl200909@163.com");

		/// <summary>
		/// 系统提供一个默认的QQ邮箱发送账号，只要更改接收地址即可发送服务，发送成功概率比较高
		/// </summary>
		// Token: 0x040007C1 RID: 1985
		public static SoftMail MailSystemQQ = new SoftMail(delegate(SmtpClient mail)
		{
			mail.Host = "smtp.qq.com";
			mail.UseDefaultCredentials = true;
			mail.Port = 587;
			mail.EnableSsl = true;
			mail.DeliveryMethod = SmtpDeliveryMethod.Network;
			mail.Credentials = new NetworkCredential("974856779", "tvnlczxdumutbbic");
		}, "974856779@qq.com", "hsl200909@163.com");
	}
}
